/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.cux.workflow.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;

import com.erp.cux.flexfield.dao.model.CuxFlexFieldLine;
import com.erp.cux.workflow.dao.data.DataBox;
import com.erp.cux.workflow.dao.model.CuxWorkflowConfLine;
import com.erp.cux.workflow.dao.model.CuxWorkflowConfLineCO;
import com.erp.cux.workflow.service.CuxWorkflowConfLineService;
import com.erp.dataset.dao.model.SysDatasetType;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.dataset.service.SysDatasetTypeService;
import com.erp.hr.dao.model.HrDepartment;
import com.erp.hr.dao.model.HrPosition;
import com.erp.hr.dao.model.HrStaff;
import com.erp.hr.service.HrCommonService;
import com.erp.hr.service.HrDepartmentService;
import com.erp.hr.service.HrPositionService;
import com.erp.hr.service.HrStaffService;
import com.erp.masterdata.common.service.MasterDataCommonService;

@Controller
@RequestMapping("/web/cuxWorkflowConfLine")
public class CuxWorkflowConfLineWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(CuxWorkflowConfLineWebController.class);
    
    //服务层注入
    @Autowired
    private CuxWorkflowConfLineService cuxWorkflowConfLineService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private SysDatasetTypeService sysDatasetTypeService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private HrDepartmentService hrDepartmentService;
    @Autowired
    private HrPositionService hrPositionService;
    @Autowired
    private HrStaffService hrStaffService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getCuxWorkflowConfLineList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-02-27 17:17:41
     * @author 
     * @param pages
     * @param cuxWorkflowConfLineCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxWorkflowConfLineList")
    public String getCuxWorkflowConfLineList(Pages pages, CuxWorkflowConfLineCO cuxWorkflowConfLineCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(100);
        
        //分页查询数据
        List<CuxWorkflowConfLine> cuxWorkflowConfLineList = this.cuxWorkflowConfLineService.getCuxWorkflowConfLineListByHeadCode(pages, cuxWorkflowConfLineCO);
        //循环获取描述字段
        for(CuxWorkflowConfLine cuxWorkflowConfLine: cuxWorkflowConfLineList) {
            try {
                if(cuxWorkflowConfLine.getApproveDepartment().contentEquals("sys_wf_department0")) {
                    cuxWorkflowConfLine.setApproveDepartmentDesc("提交人部门--工作流");
                }else if(cuxWorkflowConfLine.getApproveDepartment().contentEquals("sys_wf_department1")) {
                    cuxWorkflowConfLine.setApproveDepartmentDesc("提交人上级部门--工作流");
                }else if(cuxWorkflowConfLine.getApproveDepartment().contentEquals("sys_wf_department2")) {
                    cuxWorkflowConfLine.setApproveDepartmentDesc("提交人上上级部门--工作流");
                }else {
                    cuxWorkflowConfLine.setApproveDepartmentDesc(this.hrCommonService.getHrDepartment(cuxWorkflowConfLine.getApproveDepartment()).getSegmentDesc());
                }
            }catch(Exception e) {}
            
            try {
                cuxWorkflowConfLine.setApprovePersonDesc(this.hrCommonService.getHrStaff(cuxWorkflowConfLine.getApprovePerson()).getStaffName());
            }catch(Exception e) {}
            
            try {
                cuxWorkflowConfLine.setApprovePositionDesc(this.hrCommonService.getHrPosition(cuxWorkflowConfLine.getApprovePosition()).getPositionName());
            }catch(Exception e) {}
            
            try {
                if(cuxWorkflowConfLine.getParentTaskCode().contentEquals("sys_wf_task_start")) {
                    cuxWorkflowConfLine.setParentTaskName("开始");
                }else {
                    cuxWorkflowConfLine.setParentTaskName(this.cuxWorkflowConfLineService.getDataObject(cuxWorkflowConfLine.getParentTaskCode()).getTaskName());
                }
            }catch(Exception e) {}
        }
        
        //页面属性设置
        model.addAttribute("cuxWorkflowConfLineList", cuxWorkflowConfLineList);
        model.addAttribute("pages", pages);
        
        return "cuxWorkflow/tab/lineTab";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-02-27 17:17:41
     * @author 
     * @param cuxWorkflowConfLine
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxWorkflowConfLine")
    public String getCuxWorkflowConfLine(CuxWorkflowConfLine cuxWorkflowConfLine, Model model) {
        //查询要编辑的数据
        if(cuxWorkflowConfLine!=null&&cuxWorkflowConfLine.getConfLineId()!=null) {
            cuxWorkflowConfLine = this.cuxWorkflowConfLineService.getDataObject(cuxWorkflowConfLine.getConfLineId());
        }
        
        //获取部门
        List<HrDepartment> hrDepartmentList = this.hrDepartmentService.getDataObjects();
        //获取岗位
        List<HrPosition> hrPositionList = this.hrPositionService.getDataObjects();
        //获取职员
        List<HrStaff> hrStaffList = this.hrStaffService.getDataObjects();
        //获取当前工作流的任务
        CuxWorkflowConfLineCO cuxWorkflowConfLineCO = new CuxWorkflowConfLineCO();
        cuxWorkflowConfLineCO.setConfHeadCode(cuxWorkflowConfLine.getConfHeadCode());
        List<CuxWorkflowConfLine> cuxWorkflowConfLineList = this.cuxWorkflowConfLineService.getDataObjects(cuxWorkflowConfLineCO);
        
        
        //页面属性设置
        model.addAttribute("cuxWorkflowConfLine", cuxWorkflowConfLine);
        model.addAttribute("hrDepartmentList", hrDepartmentList);
        model.addAttribute("hrPositionList", hrPositionList);
        model.addAttribute("hrStaffList", hrStaffList);
        model.addAttribute("cuxWorkflowConfLineList", cuxWorkflowConfLineList);
        
        return "cuxWorkflow/pop/addLineModal";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-02-27 17:17:41
     * @author 
     * @param cuxWorkflowConfLine
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editCuxWorkflowConfLine")
    @ResponseBody
    public String editCuxWorkflowConfLine(@Valid CuxWorkflowConfLine cuxWorkflowConfLine, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            this.cuxWorkflowConfLineService.insertOrUpdateDataObject(cuxWorkflowConfLine);
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-02-27 17:17:41
     * @author 
     * @param cuxWorkflowConfLine
     * @return String
     *
     */
    @RequestMapping("deleteCuxWorkflowConfLine")
    @ResponseBody
    public String deleteCuxWorkflowConfLine(CuxWorkflowConfLine cuxWorkflowConfLine, RedirectAttributes attr) {
        try {
            //删除数据前验证数据
            if(cuxWorkflowConfLine!=null&&cuxWorkflowConfLine.getConfLineId()!=null) {
                //删除数据
                this.cuxWorkflowConfLineService.deleteDataObject(cuxWorkflowConfLine);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
}
